/**
 * 依赖全局变量 InfoGlobal
 * Created by shopnc on 2015/11/26.
 */
//定义表格
var dtGridColumns = [
    {
        id: 'delFlag',
        type: 'int',
        hideQuery:true,
        hideQueryType:'eq',
        hideQueryValue:1,
        hide:true
    },
    {
        id: 'user.userName',
        title: '提问用户名',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastQuery: true,
        fastQueryType: 'lk',
        fastSort: false
    },
    {
        id: 'user.mobile',
        title: '提问用户手机号',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'question',
        type: 'string',
        title: '提问内容',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        fastQuery: false,
        hideType: 'md|sm|xs|lg',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            if (record.question != undefined) {
                return "<xmp>" + record.question + "</xmp>";
            } else {
                return '';
            }
        }

    },
    {
        id: 'answer',
        type: 'string',
        title: '回复内容',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        fastQuery: false,
        hideType: 'md|sm|xs|lg',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            if(record.answer != undefined){
                return  "<xmp>"+record.answer+"</xmp>";
            }else{
                return  '';
            }
        }
    },
    {
        id: 'admin.name',
        type: 'string',
        title: '回复用户名',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        fastQueryType: 'lk',
        fastQuery: true,
    },
    {
        id: 'status',
        title: '状态',
        type: 'string',
        columnClass: 'text-center width-150',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'eq',
        hideType: 'md|sm|xs',
        codeTable: {
            1: $lang.questionStatus.T1,
            2: $lang.questionStatus.T2
        }
    },
    {
        id: 'replyCostStr',
        title: '回复用时',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        fastQuery: false,
        hideType: 'md|sm'
    },
    {
        id: 'createTime',
        title: '提问时间',
        type: 'date',
        format: 'yyyy-MM-dd',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastQuery: true,
        fastQueryType: 'range'
    },
    {
        id: 'operation',
        title: '管理操作',
        type: 'string',
        columnClass: 'text-center width-400',
        fastSort: false,
        extra: false,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var html = "";
            if ($("#questiosReply") && $("#questiosReply").val() == 1) {
                if(record.status == 1) {
                    html += "<a data-target='#replyModal' class='btn btn-sm btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "' ><i class='fa fa-commenting-o'></i>&nbsp;回复&nbsp;</a>";
                }
            }
            return html;
        }
    }
];
var dtGridOption = {
    lang: 'zh-cn',
    ajaxLoad: true,
    loadURL: ncGlobal.adminRoot + 'questions/json/list',
    exportFileName: '在线问答',
    columns: dtGridColumns,
    gridContainer: 'dtGridContainer',
    toolbarContainer: 'dtGridToolBarContainer',
    tools: 'refresh|faseQuery',
    pageSize: 10,
    pageSizeLimit: [10, 20, 50],
    ncColumnsType: {int: ["id","status","delFlag"], Timestamp: ["createTime","releaseTime"]},
    onGridComplete: function (grid) {
    }
};
var grid = $.fn.DtGrid.init(dtGridOption);
grid.sortParameter.columnId = ['asc_status','desc_createTime'];

grid.fastQueryParameters = new Object();
grid.fastQueryParameters['eq_delFlag'] = 1;
/**
 * 公告列表所使用的js
 */
var information = function () {

    /**
     * 刷新addmodal 上的图片
     */

    /**
     * 事件绑定
     * @private
     */
    function _bindEvent() {
        //模糊搜索
        $('#customSearch').click(function () {
            grid.fastQueryParameters['lk_user.user_name_or_lk_user.mobile'] = $('#keyword').val();
            grid.pager.startRecord = 0;
            grid.pager.nowPage = 1;
            grid.pager.recordCount = -1;
            grid.pager.pageCount = -1;
            grid.refresh(true);
        });


        // 新增公告对话框
        $("#addForm").on("nc.formSubmit.success", function (e) {
        })

        //添加微信事件时显示时
        $('#addModal').on('show.bs.modal', function(event) {

        });

        // bycj [ 回复对话框显示时调用 ]
        $('#replyModal').on('show.bs.modal', function (event) {
            var    //获取接受事件的元素
                button = $(event.relatedTarget),
                //获取data 参数
                datano = button.data('no'),
                modal = $(this),
                //获取列表框中的原始数据
                gridData = grid.sortOriginalDatas[datano],
                replyForm = $("#replyForm");
            replyForm.find('input[name="id"]').val(gridData.id);
            $('#userName').val(gridData.user.userName);
            $('#contentQuestion').val(gridData.question);
            $('#contentReply').val("");
            //清除错误提示
            replyForm.psly().reset();
            $(".alert-danger").remove()
        });
    }

    //外部可调用
    return {
        init: function () {
            _bindEvent();
        }
    }
}()
$(function () {
    grid.load();
    information.init();
})
